myApp = angular.module('myApp', [
    'ui.router',
    'ngStorage',
    'ngCookies',
    'ui.bootstrap',
    'widget.scrollbar',
    'ui.tree'

]);
myApp.constant('requestContext', 'http://192.168.1.171:8080/rearServiceManagement/');

myApp.controller("MainController",function($rootScope,$scope){
    $rootScope.defaultPage = "systems";
    $rootScope.systemHeight=$(window).height();
    $rootScope.systemWidth=$(window).width();
	$rootScope.$on('changeTitle',function (event,data) {
		$scope.title=data.data;
	})
});

myApp.run(function ($rootScope, $state,$stateParams,UserService,commonUtil) {

   /* $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        if (toState.name == 'login') return;//如果是进入登录页面则允许
        // console.log(UserService);
        //如果用户不存在
        // if (!$rootScope.user || $rootScope.user.token=="") {/!*|| $rootScope.user.token==""*!/
        var user=commonUtil.getCookie('user');
        if (!user||user.token==''||user.token==undefined) {
            event.preventDefault();//取消默认跳转行为
            $state.go("login", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
        }

    });*/

    $rootScope.$on('userIntercepted',function(errorType) { // 跳转到登录界面，这里我记录了一个from，这样可以在登录后自动跳转到未登录之前的那个界面
        $state.go("login", {from: $state.current.name, w: errorType});
        // console.log(UserService);
        // if ($rootScope.user.token!="") {/*.token!=""*/
        debugger
        var user=commonUtil.getCookie('user');
        if (user.token) {
            $state.go($rootScope.defaultPage);
            return;
        }
        var from = $stateParams["from"];
        $state.go(from && from != "login" ? from : $rootScope.defaultPage);
    });
    
   


    /*返回历史记录*/
    $rootScope.back = function () {
        window.history.back();
    };
    /*检验字符是否为空*/
    $rootScope.checkStrNull = function (str) {
        if (str === null || str === undefined) {
            $rootScope.tip = {flag: true, msg: '不能为空'};
        } else if (str.length === 0 || str.trim().length === 0) {
            $rootScope.tip = {flag: true, msg: '不能为空'};
        } else {
            $rootScope.tip = {flag: false, msg: ''};
        }
        return $rootScope.tip.flag;
    };
    /*检验地址对象是否为空*/
    $rootScope.checkAdrNull = function (Adr) {
        if (Adr === null || Adr === undefined) {
            $rootScope.tip = {flag: true, msg: '请选择'};
        } else if (Adr.name === null || Adr.name === undefined || Adr.name.length === 0) {
            $rootScope.tip = {flag: true, msg: '请选择'};
        } else {
            $rootScope.tip = {flag: false, msg: ''};
        }
        return $rootScope.tip.flag;
    };
});
myApp.factory('UserService',['$rootScope',function($rootScope){
    return {
        user:{
            username:'',
            password:'',
            token:''
        },
        isLogin:false
    }
}])/*.factory('UserInterceptor',["$q","$rootScope",function($q,$rootScope,UserService,commonUtil) {
    return {
        request: function(config) {
            if(commonUtil){
                var user=commonUtil.getCookie('user');
                config.headers["TOKEN"] = user.token;
            }
            console.log("request",config);
            return config;
        },
        // 请求发出时出错
        requestError: function(err){
            return $q.reject(err);
        },
        // 成功返回了响应
        response: function(res){
            console.log("response",res);
            return res;
        },
        responseError: function(response) {
            var data = response.data;
            console.log("responseError",response);
            if(!data){
                $rootScope.$emit("userIntercepted","notLogin",response);
            }
            //判断出错误码，如果是未登录
            if(data["errorCode"] == "500999") {
                //清空本地token存储信息
                var user=commonUtil.removeCookie('user');
                // UserService.user = {token: ""};
                //全局事件，方便其他view获取该事件，并给以相应的提示或处理
                $rootScope.$emit("userIntercepted","notLogin",response)
            }
            //如果是登录超时
            if(data["errorCode"] == "500998") {
                $rootScope.$emit("userIntercepted","sessionOut",response);
            }
            return $q.reject(response);
        }
    }
}])*/

